#include "bdialog.h"
#include "ui_bdialog.h"

#include <QDebug>

BDialog::BDialog(int flag, GraphicInfo *GI, QWidget *parent) :
    flag(flag), GI(GI), QDialog(parent), ui(new Ui::BDialog)
{
    ui->setupUi(this);

    ui->lineN->setValidator(new QIntValidator(1, 10, this));

    connect(ui->lineN, &QLineEdit::textChanged, this, &BDialog::SetInput);
    connect(ui->lineM, &QLineEdit::textChanged, this, &BDialog::SetInput);
    connect(ui->pushButtonOK, &QPushButton::clicked, this, &BDialog::onOK);

    if(flag == bspL){
        ui->M->setDisabled(0);
        ui->lineM->setDisabled(0);
        ui->lineM->setValidator(new QIntValidator(1, 10, this));
    }

}

BDialog::~BDialog()
{
    delete ui;
}

void BDialog::onOK()
{
    int N = ui->PIFrame->count();
    GI->B_part = ui->lineM->text().toInt();
    GI->ctrlPoint.resize(N);qDebug() << GI->ctrlPoint.size();
    for(int i = 0; i < N; i++){
        Point2Input *pi = (Point2Input *)ui->PIFrame->itemAt(i)->widget();
        GI->ctrlPoint[i].setX(pi->GetX());
        GI->ctrlPoint[i].setY(pi->GetY());
    }
//    GI->ctrlPoint.resize(3);
//    GI->ctrlPoint[0].setX(50);
//    GI->ctrlPoint[0].setY(300);

//    GI->ctrlPoint[1].setX(350);
//    GI->ctrlPoint[1].setY(50);

//    GI->ctrlPoint[2].setX(500);
//    GI->ctrlPoint[2].setY(350);

//    qDebug() << "ac";
    this->accept();
}

void BDialog::SetInput(const QString &string)
{
    int n = ui->lineN->text().toInt() + 1;
    if(sender() == ui->lineM){
        n += string.toInt() - 1;
    }
    for(int i = ui->PIFrame->count();i != n; i = ui->PIFrame->count()){
        if(i < n)
            ui->PIFrame->addWidget(new Point2Input);
        else{
            ui->PIFrame->itemAt(i - 1)->widget()->setParent(0);
            delete ui->PIFrame->itemAt(i - 1);
        }
    }
}
